package dk.codeunited.kulturarv.data.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mixare.lib.marker.Marker;

import dk.codeunited.kulturarv.data.DataSource;
import dk.codeunited.kulturarv.log.LogBridge;
import dk.codeunited.kulturarv.marker.SocialMarker;

/**
 * A data processor for twitter urls or data, Responsible for converting raw
 * data (to json and then) to marker data.
 * 
 * @author A. Egal
 * @author mixare
 * @author Kostas Rutkauskas
 */
public class TwitterDataProcessor implements DataProcessor {

	private static final int MAX_JSON_OBJECTS = 1000;

	@Override
	public String[] getUrlMatch() {
		String[] str = { "twitter" };
		return str;
	}

	@Override
	public String[] getDataMatch() {
		String[] str = { "twitter" };
		return str;
	}

	@Override
	public boolean matchesRequiredType(String type) {
		if (type.equals(DataSource.DataSourceType.TWITTER.name())) {
			return true;
		}
		return false;
	}

	@Override
	public List<Marker> load(String rawData, int taskId, int colour,
			DataSource dataSource) throws JSONException {
		List<Marker> markers = new ArrayList<Marker>();
		JSONObject root = convertToJSON(rawData);
		JSONArray dataArray = root.getJSONArray("results");
		int top = Math.min(MAX_JSON_OBJECTS, dataArray.length());

		for (int i = 0; i < top; i++) {
			JSONObject jo = dataArray.getJSONObject(i);

			Marker ma = null;
			if (jo.has("geo")) {
				Double lat = null, lon = null;

				if (!jo.isNull("geo")) {
					JSONObject geo = jo.getJSONObject("geo");
					JSONArray coordinates = geo.getJSONArray("coordinates");
					lat = Double.parseDouble(coordinates.getString(0));
					lon = Double.parseDouble(coordinates.getString(1));
				} else if (jo.has("location")) {

					// Regex pattern to match location information
					// from the location setting, like:
					// iPhone: 12.34,56.78
					// ÜT: 12.34,56.78
					// 12.34,56.78

					Pattern pattern = Pattern
							.compile("\\D*([0-9.]+),\\s?([0-9.]+)");
					Matcher matcher = pattern.matcher(jo.getString("location"));

					if (matcher.find()) {
						lat = Double.parseDouble(matcher.group(1));
						lon = Double.parseDouble(matcher.group(2));
					}
				}
				if (lat != null) {
					LogBridge.debug("processing Twitter JSON object");
					String user = jo.getString("from_user");
					String url = "http://twitter.com/" + user;

					// no ID is needed, since identical tweet by identical user
					// may be
					// safely merged into one.
					ma = new SocialMarker("", user + ": "
							+ jo.getString("text"), lat, lon, 0, url, taskId,
							colour, dataSource);
					markers.add(ma);
				}
			}
		}
		return markers;
	}

	private JSONObject convertToJSON(String rawData) {
		try {
			return new JSONObject(rawData);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
}